package com.applcn.user.config;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import feign.RequestInterceptor;
import io.seata.core.context.RootContext;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collection;
import java.util.List;
import java.util.Map;

@Configuration
public class ClientConfiguration {

	@Bean
	public RequestInterceptor headerInterceptor() {
		return template -> {
			String xid = RootContext.getXID();
			if (StringUtils.isNotEmpty(xid)) {
				List<String> xids = Lists.newArrayList(xid);
				Map<String, Collection<String>> headers = ImmutableMap.of(RootContext.KEY_XID, xids);
				template.headers(headers);
			}
		};
	}
}